package com.bh.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.ServletWebRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Wang
 * @version 1.0
 * @date 2022/6/9 15:23
 */
public class ZuulErrorController implements ErrorController {

    @Autowired
    ErrorAttributes errorAttributes;

    @Override
    public String getErrorPath() {
        return "/error";
    }

    @RequestMapping("/error")
    public Map error(HttpServletRequest request){
        Map<String, Object> map=errorAttributes.getErrorAttributes(new ServletWebRequest(request), ErrorAttributeOptions.defaults());
        Map rs=new HashMap();
        String msg="服务器开小差了····";
        rs.put("code",0);
        Object obj=map.get("message");
        if (null!=obj&&"".equals(obj.toString())){
            msg=obj.toString();
        }
        rs.put("msg",msg);
        return rs;
    }
}
